package org.arestaurant;

import java.io.IOException;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URL;
import java.util.Collection;
import java.util.Iterator;
import java.util.Map;

public class ConnectionServices {
    public static String HTTP_HOST;
    public static String HTTPS_HOST;
    public static HttpURLConnection HTTP_CONNECTION;
    public static boolean HTTP_CONNECTION_STATUS = false;
    public static int HTTP_CONNECTION_TIME_OUT = 3600;
    public static HttpURLConnection connectHttp(String requestUri) {
        String httpUrl = HTTP_HOST + requestUri;
        URL url;
        try {
            url = new URL(httpUrl);
            HTTP_CONNECTION = (HttpURLConnection) url.openConnection();
            HTTP_CONNECTION.setConnectTimeout(HTTP_CONNECTION_TIME_OUT);
            HTTP_CONNECTION.connect();
        } catch (MalformedURLException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        return HTTP_CONNECTION;
    }
    public static HttpURLConnection connectHttps(String requestUri) {
        String httpUrl = HTTPS_HOST + requestUri;
        URL url;
        try {
            url = new URL(httpUrl);
            HTTP_CONNECTION = (HttpURLConnection) url.openConnection();
        } catch (MalformedURLException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        return HTTP_CONNECTION;
    }
    public static HttpURLConnection connectHttp(String requestUri, Map<String, String> parameters) {
        String httpUrl = HTTP_HOST + requestUri;
        if (parameters.size() > 0) {
            httpUrl = httpUrl + "?";
            Collection<String> coll = parameters.keySet();
            Iterator<String> iter = coll.iterator();
            Boolean isFirst = true;
            while (iter.hasNext()) {
                String key = (String) iter.next();
                if (isFirst) {
                    httpUrl = httpUrl + key + "=" + parameters.get(key);
                } else {
                    httpUrl = httpUrl + "&" + key + "=" + parameters.get(key);
                }
            }
        }
        URL url;
        try {
            url = new URL(httpUrl);
            HTTP_CONNECTION = (HttpURLConnection) url.openConnection();
        } catch (MalformedURLException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        return HTTP_CONNECTION;
    }    
    public static void disconnect() {
        HTTP_CONNECTION.disconnect();
    }
    public static boolean checkHttpConnection() {
        int responseCode = 0;
        if (HTTP_HOST == null) HTTP_HOST = Setting.getHttpSetting();
        if (!HTTP_CONNECTION_STATUS) {
            HTTP_CONNECTION = connectHttp("/arestaurant/control/main");
            try {
                responseCode = HTTP_CONNECTION.getResponseCode();
            } catch (IOException e) {
                return false;
            }
            HTTP_CONNECTION.disconnect();
        }
        if (responseCode == 200 || HTTP_CONNECTION_STATUS) {
            HTTP_CONNECTION_STATUS = true;
            return true;
        }
        return false;
    }
    public static boolean checkHttpsConnection() {
        if (HTTPS_HOST == null) HTTPS_HOST = Setting.getHttpsSetting();
        if (HTTP_CONNECTION == null) {
            HTTP_CONNECTION = connectHttps("/aretaurant/control/main");
            HTTP_CONNECTION.disconnect();
        }
        if (HTTP_CONNECTION != null) return true;
        return false;
    }
}